//
//  VRVoiceViewController.swift
//  VoiceRoom
//
//  Created by Rae on 2024/3/11.
//

import UIKit

class VRProfileViewController: VRBaseViewController {
    
    private var topView = VRProfileTopView()
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.backgroundColor = .clear
        tableView.tableFooterView = UIView(frame: .zero)
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.estimatedRowHeight = 410
        tableView.register(VRDiscoverCell.self)
        return tableView
    }()
    var dataSource:[InfoUtil.room] = []
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        loadData()
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            tableView
        }
        
        tableView.fillContainer()
        topView.frame = CGRect(x: 0, y: 0, width: .screenWidth, height: 325)
        tableView.tableHeaderView = topView
    }

    private func loadData(){
        
        RT.getRoomList { list in
            self.dataSource.removeAll()
            for item in list{
                if item?.houseName == "___*VoiceRoom" && item?.info0 == "VoiceRoom___*Story" && item?.createUserId == USEINFOMODEL.id {
                    self.dataSource.append(item!)
                }
            }
            self.tableView.reloadData()
        }
        
        if let userId = USEINFOMODEL.id {
            RT.getOtherUserInfoUrlInfoWith(userid: userId) { jsonData in
                USEINFOMODEL = jsonData
                let userdefalut = UserDefaults.standard
                userdefalut.setValue(jsonData.toJSON(), forKey: "UserInfo")
                self.topView.updateUI()
            }
        }
        
        RT.FriendListAction { list in
            self.topView.followingCount = list.count
        }
        
        RT.fansListAction { list in
            self.topView.followerCount = list.count
        }
    }
}

extension VRProfileViewController: UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.cell(VRDiscoverCell.self, indexPath: indexPath)
        cell.profileModel = dataSource[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//        dataSource[indexPath.row].select = !dataSource[indexPath.row].select
//        tableView.reloadData()
        
    }
}
